package com.codingman.springbootdemo4;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
	@RequestMapping("/hello/{name}")
	public @ResponseBody String hello(@PathVariable String name) {
		if(name.equals("bob"))
			throw new BobNotExistsException("bob is not exists");
		else if(name.equals("candy"))
			throw new CandyNotExistsException("candy is not exists");
		else if(name.equals("dog"))
			throw new RuntimeException("dog is not a name");
		
		return "hello "+name;
	}
	
	@ExceptionHandler(BobNotExistsException.class)
	@ResponseBody String handleCustomException(HttpServletRequest request, BobNotExistsException ex) {
        return ex.toString();
    }
}
